<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\index\controller\Wechat;
use fast\Random;

/**
 * Token接口
 * @ApiSector (token管理)
 * @ApiWeight (100)
 */
class Token extends Api
{
    protected $noNeedLogin = ['wechatJsConfig'];
    protected $noNeedRight = '*';

    /**
     * @ApiTitle (检测Token是否过期)
     * @ApiHeaders (name=token,type=string,required=true,description=token)
     */
    public function check()
    {
        $token = $this->auth->getToken();
        $tokenInfo = \app\common\library\Token::get($token);
        $this->success('', ['token' => $tokenInfo['token'], 'expires_in' => $tokenInfo['expires_in']]);
    }

    /**
     * @ApiTitle (刷新Token),
     * @ApiHeaders (name=token,type=string,required=true,description=token)
     * @ApiReturnParams (name=msg,type=string,required=true,sample=请求成功)
     * @ApiReturnParams (name=data,type=object,sample={})
     */
    public function refresh()
    {
        //删除源Token
        $token = $this->auth->getToken();
        \app\common\library\Token::delete($token);
        //创建新Token
        $token = Random::uuid();
        \app\common\library\Token::set($token, $this->auth->id, 2592000);
        $tokenInfo = \app\common\library\Token::get($token);
        $this->success('', ['token' => $tokenInfo['token'], 'expires_in' => $tokenInfo['expires_in']]);
    }
    #获取jsconfig
    public function wechatJsConfig(){
        $url = $this->request->param('url');
        $options = array(
            'token'=>'', //填写你设定的key
            'appid'=> get_addon_config('epay')['wechat']['app_id'], //填写高级调用功能的app id, 请在微信开发模式后台查询
            'appsecret'=> get_addon_config('epay')['wechat']['app_secret'], //填写高级调用功能的密钥
            'encodingaeskey'=>'8AR1O5sz6t57uevKJzDfmcZyR7EessBhXd1mlq1Rxo4', //填写高级调用功能的密钥
        );
        $wechat = new Wechat($options);
        $jsconfig = $wechat->getJsSign($url);
        $this->success('',$jsconfig);
    }
}
